﻿using System;
using System.Linq;
using System.Text;
using GNUGo.Net.WebApp;
using GNUGo.Net.WebApp.Entities;
using GNUGo.Net.WebApp.WsClasses;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SuperGo.TestWebApp {
	/// <summary>
	/// Summary description for InternetGoServerTest
	/// </summary>
	[TestClass]
	public class InternetGoServerTest : BaseTest {
		private readonly InternetGoServer _igs = new InternetGoServer();

		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize]
		public static void MyClassInitialize(TestContext testContext) {
		}
		//
		// Use ClassCleanup to run code after all tests in a class have run
		// [ClassCleanup()]
		// public static void MyClassCleanup() { }
		//
		// Use TestInitialize to run code before running each test 
		[TestInitialize]
		public void MyTestInitialize() {
			InitializeDatabase();
		}
		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion

		private const string USERNAME1 = "lprichar2";
		private const string PASSWORD1 = "abcdefg2";

		[TestMethod]
		public void TestCanLogIn_Valid() {
			WsResult<object> result = _igs.CanLogIn(USERNAME1, PASSWORD1);
			Assert.IsTrue(result.Success);
		}

		[TestMethod]
		public void TestCanLogIn_InvalidPassword() {
			WsResult<object> result = _igs.CanLogIn("lprichar2", "invalid");
			Assert.IsFalse(result.Success);
			Assert.IsTrue(result.ErrorMessage.ToLower().Contains("password"));
		}

		[TestMethod]
		public void TestCanLogIn_InvalidUsername() {
			WsResult<object> result = _igs.CanLogIn("slkdjfsdf", "sdfsdfsdf");
			Assert.IsFalse(result.Success);
			Assert.IsTrue(result.ErrorMessage.ToLower().Contains("username"));
		}

		[TestMethod]
		public void TestGetIgsRank() {
			string rank;
			int rating;
			bool isRated;
			
			_igs.GetIgsRank(USERNAME1, PASSWORD1, out rank, out rating, out isRated);

			Assert.IsFalse(isRated);
			Assert.IsTrue(!String.IsNullOrEmpty(rank));
		}

		[TestMethod]
		public void TestPlayGame() {
			FinalizeGameWithCapture();

			_igs.PlayGoGame(52);
			
			Assert.AreEqual(40, _igs.FinalScoreBlack);
			Assert.AreEqual(22.5M, _igs.FinalScoreWhite);
		}
	}
}
